package com.group6.exam.manager.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.manager.service.IRoleService;
import com.group6.exam.manager.service.RoleServiceImpl;

public class RoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("roleMaintain".equals(command)) {
			roleMaintain(request, response);
		} else if ("check".equals(command)) {
			check(request, response);
		} else if ("delete".equals(command)) {
			deleteRole(request, response);
		} else if ("addRole".equals(command)) {
			addRole(request, response);
		} else if ("initRole".equals(command)) {
			initRole(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("listRole".equals(command)) {
			listRole(request, response);
		} else if ("grant".equals(command)) {
			grant(request, response);
		} else if ("gotoGrant".equals(command)) {
			gotoGrant(request, response);
		} else if ("getGrant".equals(command)) {
			getGrant(request, response);
		} else if ("permisson".equals(command)) {
			permisson(request, response);
		} else {
			throw new RuntimeException("請求失敗" + command);
		}
	}

	private void permisson(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		IRoleService service = new RoleServiceImpl();
		String rightIds = request.getParameter("rightIds");
		String roleId = request.getParameter("roleId");
		boolean mark = service.grant(rightIds, roleId);
		PrintWriter out = response.getWriter();
		if (mark) {
			out.print("<script>alert('授权成功');top.location.href='login.jsp';</script>");
		} else {

		}

		out.flush();
		out.close();

	}

	/**
	 * 根据角色编号获取所有权限json字符串
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 获取角色编号
		String roleId = request.getParameter("roleId");
		IRoleService service = new RoleServiceImpl();
		PrintWriter out = response.getWriter();
		out.print(service.getMeunsByRoleId(roleId));
		out.flush();
		out.close();

	}

	private void gotoGrant(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String roleId = request.getParameter("roleId");
		request.getRequestDispatcher(
				"/view/manager/role/grant.jsp?roleId=" + roleId).forward(
				request, response);

	}

	/**
	 * 查询系统中所有权限json字符串
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void grant(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		IRoleService service = new RoleServiceImpl();
		PrintWriter out = response.getWriter();
		out.print(service.getRights());
		out.flush();
		out.close();

	}

	/**
	 * 角色信息列表
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void listRole(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		IRoleService service = new RoleServiceImpl();
		List<Map<String, Object>> roleList = service.getAllRole();
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("view/manager/role/role-grand.jsp")
				.forward(request, response);
	}

	/**
	 * 修改角色信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		String roleName = request.getParameter("roleName");
		String roleId = request.getParameter("roleId");
		IRoleService service = new RoleServiceImpl();
		int row = service.update(roleName, roleId);
		if (row > 0) {
			response.sendRedirect("RoleServlet?command=roleMaintain");
		} else {
			out.print("<script>alert('添加失败')</script>");
		}
		out.flush();
		out.close();
	}

	/**
	 * 初始化更新页面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initRole(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String roleId = request.getParameter("roleId");
		IRoleService service = new RoleServiceImpl();
		Map<String, Object> role = service.initRole(roleId);
		request.setAttribute("role", role);
		request.getRequestDispatcher("view/manager/role/role-updaterole.jsp")
				.forward(request, response);
	}

	/**
	 * 添加角色
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void addRole(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String roleName = request.getParameter("roleName");
		IRoleService service = new RoleServiceImpl();
		int row = service.addRole(roleName);
		if (row > 0) {
			response.sendRedirect("RoleServlet?command=roleMaintain");
		} else {
			out.print("<script>alert('添加失败')</script>");
		}
		out.flush();
		out.close();
	}

	/**
	 * 删除角色
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void deleteRole(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String roleId = request.getParameter("roleId");
		IRoleService service = new RoleServiceImpl();
		int row = service.deleteRole(roleId);
		if (row > 0) {
			response.sendRedirect("RoleServlet?command=roleMaintain");
		} else {
			out.print("<script>alert('删除失败')</script>");
		}
		out.flush();
		out.close();
	}

	/**
	 * 查看角色的相关资料
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void check(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 分页查询
		PageModel pageModel = new PageModel();
		// 获取当前页
		String currentPage = request.getParameter("currentPage");
		int currentPageNo = !"".equals(currentPage) && currentPage != null ? Integer
				.valueOf(currentPage) : 1;
		pageModel.setCurrentPage(currentPageNo);
		String roleId = request.getParameter("roleId");
		pageModel.setRoleId(roleId);
		IRoleService service = new RoleServiceImpl();
		PageModel paging = service.check(pageModel);

		request.setAttribute("roleId", roleId);
		request.setAttribute("paging", paging);
		request.getRequestDispatcher("view/manager/role/role-check.jsp")
				.forward(request, response);
	}

	/**
	 * 角色维护(查询全部角色信息)
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void roleMaintain(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		IRoleService service = new RoleServiceImpl();
		List<Map<String, Object>> roleList = service.getAllRole();
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("view/manager/role/role-maintain.jsp")
				.forward(request, response);
	}

}
